package daoOptimize.junit;

import bean.Customer;
import daoOptimize.CustomerDAOImpl;
import org.junit.Test;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

/*
* 对CustomerDAOImpl功能的测试
* */
public class CustomerDAOImplTest {

    CustomerDAOImpl customerDAOImpl = new CustomerDAOImpl();
    Connection connection = null;
    @Test
    public void insert() {

        try {
            connection = JDBCUtils.getConnection();
            Customer customer = new Customer("大神", "dashen@gmail.com", new Date(969984000L));
            customerDAOImpl.insert(connection, customer);
            System.out.println("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResourceConnection(connection, null);
        }
    }

    @Test
    public void deleteById() {
        try {
            connection = JDBCUtils.getConnection();
            customerDAOImpl.deleteById(connection, 21);
            System.out.println("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResourceConnection(connection, null);
        }
    }

    @Test
    public void update() {
        try {
            connection = JDBCUtils.getConnection();
            Customer customer = new Customer(18, "贝多芬", "beiduofen@126.com", new Date(1389916800L));
            customerDAOImpl.update(connection, customer);
            System.out.println("修改成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResourceConnection(connection, null);
        }
    }

    @Test
    public void getCustomerById() {
        try {
            connection = JDBCUtils.getConnection();
            Customer customer = customerDAOImpl.getCustomerById(connection, 2);
            System.out.println(customer.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResourceConnection(connection, null);
        }
    }

    @Test
    public void getAll() {
        try {
            connection = JDBCUtils.getConnection();
            List<Customer> customerList = customerDAOImpl.getAll(connection);
            customerList.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResourceConnection(connection, null);
        }
    }

    @Test
    public void getCount() {
        try {
            connection = JDBCUtils.getConnection();
            Long count = customerDAOImpl.getCount(connection);
            System.out.println("表中的记录数为:" + count);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResourceConnection(connection, null);
        }
    }

    @Test
    public void getMaxBirth() {
        try {
            connection = JDBCUtils.getConnection();
            Date maxBirth = customerDAOImpl.getMaxBirth(connection);
            System.out.println("最大生日为: " + maxBirth);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResourceConnection(connection, null);
        }
    }
}